//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CommunityServer.Controls {

	[
	ParseChildren( true ),
	PersistChildren( false ),
	DefaultProperty( "Title" ),
	ToolboxItem( false ),
	]
	public class CollapseBarItem : WebControl {

		#region Properties

		[
		Category( "Appearance" ),
		Description( "Gets or sets the title of the item." ),
		System.ComponentModel.DefaultValue( "" ),		]
		public virtual String Title {
			get {
				Object state = ViewState["Title"];
				if ( state != null ) {
					return (String)state;
				}
				if ( this.ID != null ) {
					return this.ID;
				}
				return this.UniqueID;
			}
			set {
				CollapseBar parent  = this.Parent as CollapseBar;
				if ( parent != null ) {
					String currentTitle = this.Title;
					if ( parent.CurrentItem == currentTitle ) {
						parent.CurrentItem = value;
					}
				}
				ViewState["Title"] = value;
			}
		}

		[
		Browsable( false ),
		PersistenceMode( PersistenceMode.InnerProperty ),
		]
		public ITemplate ContentTemplate {
			get {
				return _contentTemplate;
			}
			set {
				_contentTemplate = value;
				this.ChildControlsCreated = false;
				this.EnsureChildControls();
			}
		}
		private ITemplate _contentTemplate;

		[
		EditorBrowsable( EditorBrowsableState.Never ),
		]
		public override ControlCollection Controls {
			get {
				this.EnsureChildControls();
				return base.Controls;
			}
		}

		#endregion
		
		#region Lifecycle

		protected override void CreateChildControls() {
			this.Controls.Clear();
			PlaceHolder contentArea = new System.Web.UI.WebControls.PlaceHolder();
			this.Controls.Add( contentArea );
			if ( this._contentTemplate != null ) {
				_contentTemplate.InstantiateIn( contentArea );
			}
		}

		protected override void Render(HtmlTextWriter writer) {
			this.EnsureChildControls();
			this.RenderChildren( writer );
		}



		#endregion

	}
}
